package com.java.user.controller;

import com.java.param.CartListParam;
import com.java.param.PageParam;
import com.java.pojo.User;
import com.java.user.service.UserService;
import com.java.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserAdminController {

    @Autowired
    private UserService userService;

    @PostMapping("/admin/list")
    public R listPage(@RequestBody PageParam pageParam){
        return userService.listPage(pageParam);
    }

    @PostMapping("/admin/remove")
    public R userRemove(@RequestBody @Validated CartListParam cartListParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("用户id为空，无法删除！");
        }
        return userService.userRemove(cartListParam.getUserId());
    }
    @PostMapping("/admin/update")
    public R update(@RequestBody User user){

        return userService.update(user);
    }

    @PostMapping("/admin/save")
    public R save(@RequestBody User user){

        return userService.save(user);
    }
}
